//
//  registerView.m
//  DJS
//
//  Created by mac on 2017/12/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "registerView.h"
#import "myViewController.h"
#import "workbenchViewController.h"
#import "homeViewController.h"
#import "Home2ViewController.h"
#import "Workbench2ViewController.h"
#import "My2ViewController.h"
#import "WEBViewController.h"
@implementation registerView

-(void)creatView{
    


    _selectIsOrNo=2;
    _waiter_role=@"0";
    UILabel *topLabel=[UILabel new];
    [self addSubview:topLabel];
    [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(15);
    }];
    topLabel.text=@"温馨提示：律师可申请成为专家";
    topLabel.font=[UIFont systemFontOfSize:14];
    topLabel.textColor=ColorWithRGB(186, 151, 89);
    
    _imageArr = [NSMutableArray  arrayWithCapacity:0];
    
    
    UIView *xianView=[UIView new];
    [self addSubview:xianView];
    [xianView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(148);
        make.width.mas_equalTo(160);
        make.height.mas_equalTo(4);
    }];
    xianView.backgroundColor=ColorWithRGB(220, 220, 220);
    
    
    UIView *LvSiview=[UIView new];
    [self addSubview:LvSiview];
    [LvSiview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(-80);
        make.centerY.mas_equalTo(xianView);
        make.width.height.mas_equalTo(30);
    }];
    LvSiview.tag=0;
    LvSiview.layer.cornerRadius=15;
    LvSiview.backgroundColor=ColorWithRGB(220, 220, 220);
    LvSiview.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zhuanJiaOrLvSi:)];
    [LvSiview addGestureRecognizer:tapGesture];
    
    UIImageView *LvSiIamge=[UIImageView new];
    [self addSubview:LvSiIamge];
    [LvSiIamge mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(-80);
        make.top.mas_equalTo(120);
        make.width.height.mas_equalTo(60);
    }];
    LvSiIamge.image=[UIImage imageNamed:@"律师圈-"];
    LvSiIamge.tag=0;
    LvSiIamge.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zhuanJiaOrLvSi:)];
    [LvSiIamge addGestureRecognizer:tapGesture2];
    
    [_imageArr addObject:LvSiIamge];
    
    UILabel *lvShiLabel=[UILabel new];
    [self addSubview:lvShiLabel];
    [lvShiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(LvSiIamge);
        make.top.mas_equalTo(LvSiIamge.mas_bottom).mas_equalTo(15);
    }];
    lvShiLabel.text=@"成为律师";
    lvShiLabel.font=[UIFont systemFontOfSize:16];
    lvShiLabel.textColor=ColorWithRGB(149, 149, 149);
    
    
    UIView *ZhanJiaview=[UIView new];
    [self addSubview:ZhanJiaview];
    [ZhanJiaview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(80);
        make.centerY.mas_equalTo(LvSiIamge);
        make.width.height.mas_equalTo(30);
    }];
    ZhanJiaview.layer.cornerRadius=15;
    ZhanJiaview.backgroundColor=ColorWithRGB(220, 220, 220);
    ZhanJiaview.tag=1;
    ZhanJiaview.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zhuanJiaOrLvSi:)];
    [ZhanJiaview addGestureRecognizer:tapGesture3];
    
    UIImageView *zhuanJiaImgae=[UIImageView new];
    [self addSubview:zhuanJiaImgae];
    [zhuanJiaImgae mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(80);
        make.centerY.mas_equalTo(LvSiIamge);
        make.width.height.mas_equalTo(60);
    }];
    zhuanJiaImgae.image=[UIImage imageNamed:@"专家"];
    zhuanJiaImgae.hidden=YES;
    zhuanJiaImgae.tag=1;
    zhuanJiaImgae.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zhuanJiaOrLvSi:)];
    [zhuanJiaImgae addGestureRecognizer:tapGesture4];
    [_imageArr addObject:zhuanJiaImgae];
    
    UILabel *zhanJiaLabel=[UILabel new];
    [self addSubview:zhanJiaLabel];
    [zhanJiaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(zhuanJiaImgae);
        make.top.mas_equalTo(LvSiIamge.mas_bottom).mas_equalTo(15);
    }];
    zhanJiaLabel.text=@"成为专家";
    zhanJiaLabel.font=[UIFont systemFontOfSize:16];
    zhanJiaLabel.textColor=ColorWithRGB(149, 149, 149);
    
    UIImageView *phoneImage=[UIImageView new];
    [self addSubview:phoneImage];
    [phoneImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(18);
        make.height.mas_equalTo(25);
        make.top.mas_equalTo(lvShiLabel.mas_bottom).mas_equalTo(40);
    }];
    phoneImage.image=[UIImage imageNamed:@"手机-(3)"];
    
    _phoneText=[UITextField new];
    [self addSubview:_phoneText];
    [_phoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(phoneImage.mas_right).mas_equalTo(20);
        make.centerY.mas_equalTo(phoneImage);
        make.width.mas_equalTo(240);
        make.height.mas_equalTo(40);
    }];
    _phoneText.delegate=self;
    _phoneText.placeholder=@"手机号";
    _phoneText.textColor=ColorWithRGB(49, 49, 49);
    _phoneText.font=[UIFont systemFontOfSize:19];
    _phoneText.returnKeyType=UIReturnKeyDone;
    _phoneText.keyboardType=UIKeyboardTypePhonePad;
    
    
    _codeBtn=[UIButton new];
    [self addSubview:_codeBtn];
    [_codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.width.mas_equalTo(-15);
        make.height.mas_equalTo(35);
        make.width.mas_equalTo(130);
        make.centerY.mas_equalTo(_phoneText);
    }];
    _codeBtn.layer.masksToBounds=YES;
    _codeBtn.layer.cornerRadius=5;
    _codeBtn.layer.borderWidth=1;
    _codeBtn.layer.borderColor=ColorWithRGB(230, 230, 230).CGColor;
    [_codeBtn setTitle:@"发送验证码" forState:0];
    [_codeBtn setTitleColor:ColorWithRGB(49, 49, 49) forState:0];
    _codeBtn.titleLabel.font=[UIFont systemFontOfSize:16];
    [_codeBtn  addTarget:self action:@selector(huoQuYanZhen:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *xianView2=[UIView new];
    [self addSubview:xianView2];
    [xianView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(1);
        make.top.mas_equalTo(_phoneText.mas_bottom).mas_equalTo(10);
    }];
    xianView2.backgroundColor=ColorWithRGB(230, 230, 230);
    
    
    UIImageView *codeImage=[UIImageView new];
    [self addSubview:codeImage];
    [codeImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(18);
        make.height.mas_equalTo(25);
        make.top.mas_equalTo(xianView2.mas_bottom).mas_equalTo(40);
    }];
    codeImage.image=[UIImage imageNamed:@"验证码-(3)"];
    
    _codeText=[UITextField new];
    [self addSubview:_codeText];
    [_codeText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(phoneImage.mas_right).mas_equalTo(20);
        make.centerY.mas_equalTo(codeImage);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(40);
    }];
    _codeText.delegate=self;
    _codeText.placeholder=@"请输入数字验证码";
    _codeText.textColor=ColorWithRGB(49, 49, 49);
    _codeText.font=[UIFont systemFontOfSize:19];
    _codeText.returnKeyType=UIReturnKeyDone;
    _codeText.keyboardType=UIKeyboardTypePhonePad;
    
    
    
    UIView *xianView3=[UIView new];
    [self addSubview:xianView3];
    [xianView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(1);
        make.top.mas_equalTo(_codeText.mas_bottom).mas_equalTo(10);
    }];
    xianView3.backgroundColor=ColorWithRGB(230, 230, 230);
    
    
    UIImageView *pwdImage=[UIImageView new];
    [self addSubview:pwdImage];
    [pwdImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(18);
        make.height.mas_equalTo(25);
        make.top.mas_equalTo(xianView3.mas_bottom).mas_equalTo(40);
    }];
    pwdImage.image=[UIImage imageNamed:@"密码-(1)"];
    
    _pwdText=[UITextField new];
    [self addSubview:_pwdText];
    [_pwdText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(phoneImage.mas_right).mas_equalTo(20);
        make.centerY.mas_equalTo(pwdImage);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(40);
    }];
    _pwdText.delegate=self;
    _pwdText.placeholder=@"请输入6-16位数字或者字母密码";
    _pwdText.textColor=ColorWithRGB(49, 49, 49);
    _pwdText.font=[UIFont systemFontOfSize:19];
    _pwdText.returnKeyType=UIReturnKeyDone;
    _pwdText.keyboardType=UIKeyboardTypeDefault;
    _pwdText.delegate=self;
    _pwdText.secureTextEntry=YES;//密文
    

    UIView *xianView4=[UIView new];
    [self addSubview:xianView4];
    [xianView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(1);
        make.top.mas_equalTo(_pwdText.mas_bottom).mas_equalTo(10);
    }];
    xianView4.backgroundColor=ColorWithRGB(230, 230, 230);
    
    _xieYiImage=[UIImageView new];
    [self addSubview:_xieYiImage];
    [_xieYiImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(phoneImage);
        make.width.height.mas_equalTo(25);
        make.top.mas_equalTo(xianView4.mas_bottom).mas_equalTo(40);
    }];
    _xieYiImage.image=[UIImage imageNamed:@"否"];
    _xieYiImage.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture13= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buxuan:)];
    [_xieYiImage addGestureRecognizer:tapGesture13];
    
    
    UILabel *xieYiLabel=[UILabel new];
    [self addSubview:xieYiLabel];
    [xieYiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_xieYiImage.mas_right).mas_equalTo(20);
        make.centerY.mas_equalTo(_xieYiImage);
    }];
    NSMutableAttributedString *AttributedStr= [[NSMutableAttributedString alloc]initWithString:@"注册即表示您同意《独角兽平台声明》"];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName value:ColorWithRGB(149, 149, 149) range:NSMakeRange(0, 8)];
    [AttributedStr addAttribute:NSForegroundColorAttributeName value:ColorWithRGB(186, 151, 89) range:NSMakeRange(8, 9)];
    xieYiLabel.attributedText=AttributedStr;
    xieYiLabel.font=[UIFont systemFontOfSize:16];
    xieYiLabel.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture12 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToXieyi:)];
    [xieYiLabel addGestureRecognizer:tapGesture12];
    
    
    UIButton *loginBtn=[UIButton new];
    [self addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(xieYiLabel.mas_bottom).mas_equalTo(70);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(_mainW-40);
    }];
    loginBtn.layer.cornerRadius=20;
    loginBtn.backgroundColor=ColorWithRGB(189, 156, 104);
    [loginBtn setTitle:@"注册" forState:0];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:0];
    [loginBtn addTarget:self action:@selector(registerBtn:) forControlEvents:UIControlEventTouchUpInside];

}


-(IBAction)huoQuYanZhen:(UIButton *)sender{
    
    NSString *mobile=_phoneText.text;
    if (![ZQTools isMobileNumber:mobile]) {
        [ZQTools svpInfo:@"手机号错误"];
        return;
    }
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:mobile,@"phone",@"0",@"code_type",@"1",@"user_role", nil];
    [ZQTools AFNPostDataUrl:@"api/index/get_code" Dict:dic andTableView:nil andViewController:self.viewController andSuccessBlock:^(id responseObject) {
        _codeBtn.enabled=NO;
        _timeTick=60;
        _timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
        
    } anderrorBlock:nil];
    
}
-(void)buxuan:(UITapGestureRecognizer *)tap{
    if (_selectIsOrNo==1) {
        _selectIsOrNo=2;
        _xieYiImage.image=[UIImage imageNamed:@"否"];
        return;
    }if (_selectIsOrNo==2) {
        _selectIsOrNo=1;
        _xieYiImage.image=[UIImage imageNamed:@"是"];
        return;
    }
}

-(void)timeFireMethod
{
    if (_timeTick==0) {
        _codeBtn.enabled=YES;
        [_codeBtn setTitle:@"发送验证码" forState:0];
        _codeBtn.backgroundColor=_backgroundColor;
        [_timer invalidate];
        return;
    }
    _timeTick--;
    [_codeBtn setTitle:[NSString stringWithFormat:@"%dS重发",_timeTick] forState:0];
    _codeBtn.backgroundColor=_DarkGrey;
}

-(void)zhuanJiaOrLvSi:(UITapGestureRecognizer *)tap{
    _waiter_role=[NSString stringWithFormat:@"%ld",tap.view.tag];
    for (int i=0; i<_imageArr.count; i++) {
        UIImageView *iamgeView=_imageArr[i];
        if (tap.view.tag==i) {
            iamgeView.hidden=NO;
        }
        else{
             iamgeView.hidden=YES;
        }
    }
    
}
-(IBAction)registerBtn:(UIButton *)btn{
    
    
    
    NSString *waiter_phone=_phoneText.text;
    NSString *waiter_pass=_pwdText.text;
    NSString *codeStr=_codeText.text;
    
    
    if (![ZQTools isMobileNumber:waiter_phone]) {
        [ZQTools svpInfo:@"手机号有误"];
        return;
    }
    if ([ZQTools charIsNil:codeStr]) {
        [ZQTools svpInfo:@"验证码不能为空"];
        return;
    }
    if ([ZQTools charIsNil:waiter_pass]||waiter_pass.length<6||waiter_pass.length>16) {
        [ZQTools svpInfo:@"密码必须是6到16位字符组成"];
        return;
    }
    
    if (_selectIsOrNo==2) {
        [ZQTools svpInfo:@"你还没有同意注册协议"];
        return;
    }
   
    
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:waiter_phone,@"waiter_phone",waiter_pass,@"waiter_pass",_waiter_role,@"waiter_role",codeStr,@"verify_code", nil];
    [ZQTools AFNPostDataUrl:@"waiter/Logreg/register" Dict:dic andTableView:nil andViewController:self.viewController andSuccessBlock:^(id responseObject) {
        
        [NSKeyedArchiver archiveRootObject:responseObject toFile:_userModelFile];
        NSNumber *waiter_role=[responseObject objectForKey:@"waiter_role"];
        NSNumber *waiter_id1=[responseObject objectForKey:@"waiter_id"];
        NSString *waiter_id=[NSString stringWithFormat:@"%@",waiter_id1];
        [JPUSHService setAlias:waiter_id completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            NSLog(@"%@",iAlias);
            if (iResCode == 0) {
                NSLog(@"添加别名成功");
            }
        } seq:1];
       
        if (waiter_role.intValue==0) {
            homeViewController *shouye=[[homeViewController alloc] init];
            
            workbenchViewController *huDong=[[workbenchViewController alloc] init];
            
            myViewController *youJi=[[myViewController alloc] init];
            
            UINavigationController *nav1= [[UINavigationController alloc] initWithRootViewController:shouye];
            UINavigationController *nav2= [[UINavigationController alloc] initWithRootViewController:huDong];
            UINavigationController *nav3= [[UINavigationController alloc] initWithRootViewController:youJi];
            
            nav1.title=@"首页";
            nav2.title=@"工作台";
            nav3.title=@"我的";
            
            [nav1.tabBarItem setSelectedImage:[UIImage imageNamed:@"首页1"]];
            [nav1.tabBarItem setImage:[UIImage imageNamed:@"首页"]];
            
            [nav2.tabBarItem setSelectedImage:[UIImage imageNamed:@"工作台1"]];
            [nav2.tabBarItem setImage:[UIImage imageNamed:@"工作台"]];
            
            [nav3.tabBarItem setSelectedImage:[UIImage imageNamed:@"我的1"]];
            [nav3.tabBarItem setImage:[UIImage imageNamed:@"我的"]];
            
            [[UITabBar appearance] setTintColor:_backgroundColor];
            
            UITabBarController *bar=[[UITabBarController alloc] init];
            bar.viewControllers=@[nav1,nav2,nav3];
            
            self.window.rootViewController=bar;
            [self.window makeKeyAndVisible];
        }
        else{
            Home2ViewController *shouye=[[Home2ViewController alloc] init];
            Workbench2ViewController *huDong=[[Workbench2ViewController alloc] init];
            
            My2ViewController *youJi=[[My2ViewController alloc] init];
            
            UINavigationController *nav1= [[UINavigationController alloc] initWithRootViewController:shouye];
            UINavigationController *nav2= [[UINavigationController alloc] initWithRootViewController:huDong];
            UINavigationController *nav3= [[UINavigationController alloc] initWithRootViewController:youJi];
            
            nav1.title=@"首页";
            nav2.title=@"工作台";
            nav3.title=@"我的";
            
            [nav1.tabBarItem setSelectedImage:[UIImage imageNamed:@"首页1"]];
            [nav1.tabBarItem setImage:[UIImage imageNamed:@"首页"]];
            
            [nav2.tabBarItem setSelectedImage:[UIImage imageNamed:@"工作台1"]];
            [nav2.tabBarItem setImage:[UIImage imageNamed:@"工作台"]];
            
            [nav3.tabBarItem setSelectedImage:[UIImage imageNamed:@"我的1"]];
            [nav3.tabBarItem setImage:[UIImage imageNamed:@"我的"]];
            
            [[UITabBar appearance] setTintColor:_backgroundColor];
            
            UITabBarController *bar=[[UITabBarController alloc] init];
            bar.viewControllers=@[nav1,nav2,nav3];
            
            self.window.rootViewController=bar;
            [self.window makeKeyAndVisible];
        }
        
    } anderrorBlock:nil];
    
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string{
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
-(void)pushToXieyi:(UITapGestureRecognizer *)tap{
    WEBViewController *webView=[[WEBViewController alloc] init];
    webView.title=@"用户协议";
    webView.url=@"http://www.djiaoshou.com/index/index/waiter_register_agreement.html";
    [ZQTools pushNextViewController:self.viewController andRootController:webView];
}

@end
